import json
import logging
import os


class EnvConfig:
    def __init__(self):
        wxConf = self.load_config()
        for key, value in wxConf.items():
            setattr(self, key, value)

    @staticmethod
    def load_config() -> dict | None:
        try:
            config_path = ['./config.json', './config_template.json']
            for path in config_path:
                if os.path.exists(path):
                    with open(path, 'r', encoding='utf-8') as f:
                        config_str = f.read()
                    res_config = json.loads(config_str)
                    for name, value in os.environ.items():
                        name = name.upper()
                        if name in res_config:
                            try:
                                res_config[name] = eval(value)
                            except:
                                if value == "false":
                                    res_config[name] = False
                                elif value == "true":
                                    res_config[name] = True
                                else:
                                    res_config[name] = value
                            logging.info(f"[config] Override the config by env conf {name}={value}")
                    logging.info("[INIT] Load config: {}".format(res_config))
                    return res_config
        except Exception as e:
            logging.critical("[config] Failed to load config json file, or path not exists. Process exit.")
            logging.critical(e)
            exit(1)

    def reload_config(self):
        wxConf = self.load_config()
        for key, value in wxConf.items():
            setattr(self, key, value)


envConfig = EnvConfig()


SERVER_HOST: str = 'http://192.168.3.3' if os.getenv("SERVER_HOST") is None else os.getenv("SERVER_HOST")
SERVER_PORT: int = 5005 if os.getenv("SERVER_PORT") is None else int(os.getenv("SERVER_PORT"))
DEFAULT_DATE_FORMAT: str = '%Y-%m-%d'
DEFAULT_TIME_FORMAT: str = DEFAULT_DATE_FORMAT + ' %H:%M:%S'
FILENAME_TIME_FORMAT: str = '%Y%M%d%H%M%S'
ROOT_PATH: str = os.path.dirname(__file__)
APP_DATA_DIR: str = os.path.join(ROOT_PATH, 'data')
GEN_FILE_DIR: str = os.path.join(APP_DATA_DIR, 'gen')
TEMPLATE_DIR: str = os.path.join(APP_DATA_DIR, 'templates')
TMP_FILE_ROOT_DIR: str = os.path.join(os.path.dirname(__file__), 'data', 'tmp')
LOG_DIR: str = os.path.join(ROOT_PATH, 'logs')

MINIO_BUCKET_NAME = 'kqk-bucket'

# ---WX---
HOT_RELOAD = True
GROUP_CHAT_PREFIX = [
    "@咕噜蛋",
    "咕噜蛋",
    "金桔王"
]

SINGLE_CHAT_PREFIX = [
        "咕噜蛋",
        "金桔王",
]

GROUP_CHAT_REPLY_PREFIX = os.getenv("GROUP_CHAT_REPLY_PREFIX") if os.getenv("GROUP_CHAT_REPLY_PREFIX") is not None else ""
GROUP_CHAT_REPLY_SUFFIX = os.getenv("GROUP_CHAT_REPLY_SUFFIX") if os.getenv("GROUP_CHAT_REPLY_SUFFIX") is not None else ""
SINGLE_CHAT_REPLY_PREFIX = os.getenv("SINGLE_CHAT_REPLY_PREFIX") if os.getenv("SINGLE_CHAT_REPLY_PREFIX") is not None else ""
SINGLE_CHAT_REPLY_SUFFIX = os.getenv("SINGLE_CHAT_REPLY_SUFFIX") if os.getenv("SINGLE_CHAT_REPLY_SUFFIX") is not None else ""

GROUP_NAME_WHITE_LIST = os.getenv("GROUP_NAME_WHITE_LIST") if os.getenv("GROUP_NAME_WHITE_LIST") is not None else [
        "鑫茂园",
        "测试群"
]

# NICK_NAME_BLACK_LIST = []
NO_NEED_AT = False
ACCEPT_FILE_TYPE = [
        "xlsx",
        "xls"
]
FILE_MSG_SPLITER = ";;;"
CONCURRENCY_IN_SESSION = 1
EXPIRED_IN_SECONDS = 3600


